package com.app.Activity;


import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;



import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.AccountDTO;
import com.app.DTO.RoleDTO;
import com.app.PhanQuyen.Quyen;
import com.app.outdoor.InvoikeServices.HeThongInvoike;
import com.google.android.gcm.GCMRegistrar;

public class od_DangNhapActivity extends Activity {
	Button btnDangNhap;
	TextView txtTenDangNhap, txtMatKhau, txtLoi;
	ProgressDialog mProgressDialog;
	AsyncTask<String, Void, Boolean> mDangNhapTask;
	String regId = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.od_activity_dang_nhap);
		btnDangNhap = (Button) findViewById(R.id.btnDangNhap);
		txtMatKhau = (TextView) findViewById(R.id.txtMatKhau);
		txtTenDangNhap = (TextView) findViewById(R.id.txtTenDanNhap);
		txtLoi = (TextView) findViewById(R.id.txtLoi);
		txtTenDangNhap.setText("phu");
		txtMatKhau.setText("phu");
		initProgressDialog();
		
		
		btnDangNhap.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				initDangNhapTask();
				regId = GCMRegistrar.getRegistrationId(od_DangNhapActivity.this);
				mDangNhapTask.execute(txtTenDangNhap.getText().toString(), txtMatKhau.getText().toString(), regId);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.activity_dang_nhap, menu);
		return true;
	}

	private void initDangNhapTask() {
		mDangNhapTask = new AsyncTask<String, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(String... params) {
				try {
					boolean dangKy=HeThongInvoike.register(od_DangNhapActivity.this);
					if(!dangKy){
						return false;
					}		
					params[2]=GCMRegistrar.getRegistrationId(od_DangNhapActivity.this);
					return HeThongInvoike.DangNhap(params[0], params[1], params[2]);
				} catch (Exception e) {
					return false;
				}
			}

			@Override
			protected void onPostExecute(Boolean result) {
				mProgressDialog.dismiss();
				if (result) {
					if(NetworkController._peer==null){
						AccountDTO account=new AccountDTO();
						account.set_Username(CommonUtilities.nguoiDung.getTenDangNhap());
						account.setId(CommonUtilities.nguoiDung.getId());	
						account.isThanhLapNhom=true;
						NetworkController.init("MES", 12322, account);		
						ArrayList<Quyen> ruleList=ReadWriteFileController.readRule(getApplicationContext(), ReadWriteFileController.RULE_BASE_PATH);
						RoleDTO roleSangLapNhom=new RoleDTO("Người sáng lập nhóm", ruleList);		
						NetworkController._peer.getAccount().addRole(roleSangLapNhom);
					}
					NetworkController.isOutdoor=true;
					Intent iMain=new Intent(getBaseContext(),od_MainActivity.class);
					startActivity(iMain);
				} else {
					txtLoi.setText("Thất bại!");
				}
			}

			@Override
			protected void onPreExecute() {
				mProgressDialog.show();
				NetworkController.isOutdoor=true;
			}
		};
	}

	private void initProgressDialog() {
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage("Đang kết nối tới máy chủ!");
		mProgressDialog.setButton("Hủy bỏ", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mDangNhapTask.cancel(true);
			}
		});
	}
}
